<?php
class League {
	// Déclarations des données
	var $id = NULL;
	var $sport = NULL;
	var $names = array();
	var $exist = false;
	
	// Constructeur
	function League($pId){
		global $wpdb;
		$_row = $wpdb->get_row($wpdb->prepare("SELECT * FROM leagues WHERE id = %d", $pId));
		
		$this->id = $pId;
		if($_row->id > 0){
			$this->sport = $_row->fk_sport;
			$this->exist = true;
			
			// recuperation des noms de la league
			$_row2 = $wpdb->get_results($wpdb->prepare("SELECT * FROM leaguenames WHERE fk_league = %d", $this->id));
			foreach($_row2 as $_name){
				$this->names[$_name->language] = $_name->value;
			}			
		}
		else
		{
			// création du tableau des nom de compétition avec des valeurs vides.
			foreach(getLanguages() as $_language){
				$this->names[$_language->id] = "";
			}
		}
	}
	
	// Déclarations des méthodes
	
	//======================
	// Création
	//======================
	function create(){
		$this->createLeague();
		$this->createLeagueNames();
	}
	
	function createLeague(){
		global $wpdb;
		 
		$queryTable = 'leagues';
		$queryData = array( 
			'id' => $this->id, 
			'fk_sport' => $this->sport,
		);
		$queryDataFormat = array('%d','%d');
		
		$wpdb->insert( $queryTable, $queryData, $queryDataFormat);
	}
	
	function createLeagueNames(){
		global $wpdb;
		
		foreach(getLanguages() as $_language){
			$queryTable = 'leaguenames';
			$queryData = array(  
				'fk_league' => $this->id,
				'language' => $_language->id,
				'value' => $this->names[$_language->id]
			);
			$queryDataFormat = array('%d','%s','%s');
			
			$wpdb->insert( $queryTable, $queryData, $queryDataFormat);
		}
	}

	//======================
	// Sauvegarde
	//======================
	function save(){
		$this->saveLeague();
		$this->saveLeagueNames();
	}
	
	function saveLeague(){
		global $wpdb;
		
		$queryTable = 'leagues';
		$queryData = array( 'fk_sport' => $this->sport);
		$queryDataFormat = array('%d');		
		$queryCondition = array("id" => $this->id);		
		$queryConditionFormat = array('%d');
		
		$wpdb->update( $queryTable, $queryData, $queryCondition, $queryDataFormat, $queryConditionFormat);
	}
	
	function saveLeagueNames(){
		global $wpdb;
		
		foreach(getLanguages() as $_language){
			$queryTable = 'leaguenames';
			$queryData = array( 'value' => $this->names[$_language->id]	);
			$queryDataFormat = array('%s');		
			$queryCondition = array("fk_league" => $this->id, "language" => $_language->id);		
			$queryConditionFormat = array('%d', '%s');
			
			$wpdb->update( $queryTable, $queryData, $queryCondition, $queryDataFormat, $queryConditionFormat);
		}
	}
	
	//======================
	// Get/Set
	//======================
	
	function getId(){
		return $this->id;
	}
	
	function getExist(){
		return $this->exist;
	}
	
	function setExist($pValue){
		$this->exist = $pValue;
	}
	
	function getSport(){
		return $this->sport;
	}
	
	function setSport($pSport){
		$this->sport = $pSport;
	}
	
	function getName($pLang="en"){
		return $this->names[$pLang];
	}
	
	function setName($pLang, $pName){
		$this->names[$pLang] = $pName;
	}
	
	//======================
	// Récupération du nombre de pari disponible pour cette league
	//======================
	
	function getCountBetsAvailable(){
		global $wpdb;
		return $wpdb->get_var(
				$wpdb->prepare("SELECT count(*) 
								FROM bets b 
								JOIN events e ON b.fk_event = e.id 								
								WHERE e.fk_league = %d	
								AND	(	( e.startdate > '%s')
										 OR
										( e.startdate = '%s' AND e.starthour > '%s')
									)",
				
								$this->id,
								date("Y-m-d"),
								date("Y-m-d"),
								date("H:i:s")
								
								)
							);
	}
	
	function getCountBetsAvailableForDate($pDate){
		global $wpdb;
		return $wpdb->get_var(
				$wpdb->prepare("SELECT count(*) 
								FROM bets b 
								JOIN events e ON b.fk_event = e.id 								
								WHERE e.fk_league = %d	
								AND	e.startdate = '%s'
								AND e.starthour > '%s'",
				
								$this->id,
								$pDate,
								date("H:i:s")
								
								)
							);
	}
	
	//======================
	// Récupération de la liste des events rattachés à cette league pour une date donnée
	//======================
	
	function getEvents($pDate){
		global $wpdb;
		return $wpdb->get_results(
				$wpdb->prepare("SELECT e.id
								FROM events e 
								WHERE e.fk_league = %d
								AND e.startdate = '%s' AND e.starthour > '%s'
								ORDER BY e.starthour",
				
								$this->id,
								$pDate,
								date('H:i:s')
								)
			   				);
	}
}
?>